//
//  UIViewController+Extention.m
//  pmm
//
//  Created by WTFSSD on 2017/8/5.
//  Copyright © 2017年 WTFSSD. All rights reserved.
//

#import "UIViewController+Extention.h"
#import <objc/runtime.h>
@implementation UIViewController (Extention)


-(UIViewController *)showAlertWith:(UIAlertControllerStyle)style
                             title:(NSString *)title
                           message:(NSString *)message
                           options:(NSArray<NSString *> *)options
                       cancelIndex:(NSInteger)cancelIndex
                  destructiveIndex:(NSInteger)destructiveIndex
                        textFields:(NSInteger)textFields
                   textFeildConfig:(void (^)(UITextField *, NSInteger))textFeildConfig
                     clickCallBack:(ClickCallBackHandle)clickCallBack{
     UIAlertController * alertVc = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:style];
    for (int i =0 ; i<options.count; i++) {
        NSString * actionTitle = options[i];
        UIAlertActionStyle actionStyle = UIAlertActionStyleDefault;
        if (destructiveIndex==cancelIndex) {
            
        }else if (cancelIndex == i){
            actionStyle = UIAlertActionStyleCancel;
        }
        else if (destructiveIndex == i){
            actionStyle = UIAlertActionStyleDestructive;
        }
        UIAlertAction * act = [UIAlertAction actionWithTitle:actionTitle style:actionStyle handler:^(UIAlertAction * _Nonnull action) {
            if (action.style != UIAlertActionStyleCancel) {
                if (clickCallBack) {
                    clickCallBack(action,alertVc.textFields);
                }
            }
        }];
        [alertVc addAction:act];
    }
    
    LSWeakSelf(textFeildConfig)
    
    for (int i = 0; i<textFields; i++) {
        [alertVc addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            if (weaktextFeildConfig) {
                weaktextFeildConfig(textField,i);
            }
        }];
    }
    
    [self presentViewController:alertVc animated:YES completion:nil];
    
    return self;
}


-(UIViewController *)showAlertWith:(UIAlertControllerStyle)style
                             title:(NSString *)title
                           message:(NSString *)message
                           options:(NSArray<NSString *> *)options
                       cancelIndex:(NSInteger)cancelIndex
                  destructiveIndex:(NSInteger)destructiveIndex
                     clickCallBack:(ClickCallBackHandle)clickCallBack{
    
    [self showAlertWith:style
                  title:title
                message:message
                options:options
            cancelIndex:cancelIndex
       destructiveIndex:destructiveIndex
             textFields:0
        textFeildConfig:nil
          clickCallBack:clickCallBack];
    return self;
}

-(UIViewController *)showImagePickerWith:(void (^)(UIImage *))selectedImage{
    
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    objc_setAssociatedObject(self, @selector(showImagePickerWith:), selectedImage, OBJC_ASSOCIATION_COPY);
    
    
    return [self showAlertWith:UIAlertControllerStyleActionSheet
                         title:@"请选择图片来源"
                       message:nil
                       options:@[@"从相册选择照片",@"现在拍摄",@"取消"]
                   cancelIndex:2
              destructiveIndex:-1
                 clickCallBack:^(UIAlertAction *action,NSArray * textFields) {
                if ([action.title isEqualToString:@"从相册选择照片"]) {

                    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    [self presentViewController:imagePicker animated:YES completion:nil];

                }else if ([action.title isEqualToString:@"现在拍摄"]){
                    if (IS_SIMULATOR) {

                        [self showAlertWith:UIAlertControllerStyleAlert title:@"" message:@"模拟器不支持拍照" options:@[@"取消"] cancelIndex:0 destructiveIndex:-1 clickCallBack:nil];

                    }else{

                        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                        [self presentViewController:imagePicker animated:YES completion:nil];

                    }
                }
    }];
}



-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{


    void(^callBack)(UIImage* img) = objc_getAssociatedObject(self, @selector(showImagePickerWith:));

    NSLog(@"info ===== %@",info);

    
    if (callBack) {

        callBack([info objectForKey:UIImagePickerControllerOriginalImage]);
    }

    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    [picker dismissViewControllerAnimated:YES completion:nil];

}

///
-(UIViewController *)showActionSheetWith:(NSString *)title
                                 message:(NSString *)message
                                 options:(NSArray<NSString *> *)options
                           clickCallBack:(ClickCallBackHandle)clickCallBack{
    return [self showAlertWith:UIAlertControllerStyleActionSheet
                         title:title
                       message:message
                       options:[[NSMutableArray arrayWithArray:options] arrayByAddingObject:@"取消"]
                   cancelIndex:options.count
              destructiveIndex:-1
                 clickCallBack:clickCallBack];
}

-(UIViewController *)showAlertWith:(NSString *)title
                           message:(NSString *)message
                           options:(NSArray<NSString *> *)options
                       cancelIndex:(NSInteger)cancelIndex
                  destructiveIndex:(NSInteger)destructiveIndex
                     clickCallBack:(ClickCallBackHandle)clickCallBack{
    return [self showAlertWith:UIAlertControllerStyleAlert
                         title:title message:message
                       options:options
                   cancelIndex:cancelIndex
              destructiveIndex:destructiveIndex
                 clickCallBack:clickCallBack];
}



@end
